<?php

// v sehen ,u ändern ,d löschen ,p publizieren ,a hinzufügen

class dbaPermission {

  function dbaPermission (&$dba){
    $this->dba = &$dba;
  }

  function change ($rarr,$uid,$fields){
   if ($uid){
    if (is_array($fields)){
      if (is_array($rarr)){
        foreach($rarr As $name => $val){
         $edited[] = $name;
         if ($this->exists($uid,$name))
           $this->update($uid,$name,$val);
         else
           $this->add($uid,$name,$val);
        }
      } else $edited = array();
      foreach ($fields As $field){
        if (!in_array($field,$edited)){
         if ($this->exists($uid,$field))
           $this->update($uid,$field,array('N','N','N','N','N','N'));
         else
           $this->add($uid,$field,array('N','N','N','N','N','N'));
        }
      }
    }
   }
  }

  function add ($uid,$name,$perm){
   if ($uid && $name){
    if (empty($perm['v'])) $perm['v'] = 'N';
    if (empty($perm['u'])) $perm['u'] = 'N';
    if (empty($perm['d'])) $perm['d'] = 'N';
    if (empty($perm['p'])) $perm['p'] = 'N';
    if (empty($perm['a'])) $perm['a'] = 'N';
    if (empty($perm['m'])) $perm['m'] = 'N';
    return $this->dba->query("insert into ".$this->dba->tableforwarder."_Permission (uid,name,v,u,d,p,a,m) values ('$uid','$name','".$perm['v']."','".$perm['u']."','".$perm['d']."','".$perm['p']."','".$perm['a']."','".$perm['m']."');");
   }
  }

  function del ($uid,$name){
    if ($uid && $name)
     return $this->dba->query("delete from ".$this->dba->tableforwarder."_Permission where name='$name' AND uid='$uid';");
  }

  function delByUid ($uid){
    if ($uid)
     return $this->dba->query("delete from ".$this->dba->tableforwarder."_Permission where uid='$uid';");
  }

  function update ($uid,$name,$perm){
    if ($uid && $name)
    {
     if (empty($perm['v'])) $perm['v'] = 'N';
     if (empty($perm['u'])) $perm['u'] = 'N';
     if (empty($perm['d'])) $perm['d'] = 'N';
     if (empty($perm['p'])) $perm['p'] = 'N';
     if (empty($perm['a'])) $perm['a'] = 'N';
     if (empty($perm['m'])) $perm['m'] = 'N';
     return $this->dba->query("update ".$this->dba->tableforwarder."_Permission set v='".$perm['v']."',u='".$perm['u']."',d='".$perm['d']."',p='".$perm['p']."',a='".$perm['a']."',m='".$perm['m']."' where name='$name' AND uid='$uid';");
    }
  }

  function exists ($uid,$name){
    if ($uid && $name){
     $sth = $this->dba->query("select name from ".$this->dba->tableforwarder."_Permission where name='$name' AND uid='$uid';");
     return ($sth)?$sth->num_rows():false;
    }
  }

  function getByUid($uid){
  	$permissions = array();
    if ($uid){
      $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_Permission where uid='$uid';");
      if ($sth && ($num_rows = $sth->num_rows())){
        $res = $sth->fetch_rows_Array();
        for ($i=0;$i<$num_rows;$i++){
          $rights = array('v' => $res[$i]['v'],'u' => $res[$i]['u'],'d' => $res[$i]['d'],'p' => $res[$i]['p'],'a' => $res[$i]['a'],'m' => $res[$i]['m']);
          $permissions[$res[$i]['name']] = $rights;
        }
      }
    }
    return $permissions;
  }

  function getUserGroups($uid){
  	$permissions = array();
    if ($uid){
      $sth = $this->dba->query("select * from ".$this->dba->tableforwarder."_Permission left join ".$this->dba->tableforwarder."_GroupUser on (uid = group_id) where user_id='$uid';");
      if ($sth && ($num_rows = $sth->num_rows())){
        $res = $sth->fetch_rows_Array();
        for ($i=0;$i<$num_rows;$i++){
          if (isset($permissions[$res[$i]['name']]) && is_array($permissions[$res[$i]['name']]))
          {
             if ($res[$i]['v']=='Y') $permissions[$res[$i]['name']]['v'] = 'Y';
             if ($res[$i]['u']=='Y') $permissions[$res[$i]['name']]['u'] = 'Y';
             if ($res[$i]['d']=='Y') $permissions[$res[$i]['name']]['d'] = 'Y';
             if ($res[$i]['p']=='Y') $permissions[$res[$i]['name']]['p'] = 'Y';
             if ($res[$i]['a']=='Y') $permissions[$res[$i]['name']]['a'] = 'Y';
             if ($res[$i]['m']=='Y') $permissions[$res[$i]['name']]['m'] = 'Y';
          }
          else
          {
             $rights = array('v' => $res[$i]['v'],'u' => $res[$i]['u'],'d' => $res[$i]['d'],'p' => $res[$i]['p'],'a' => $res[$i]['a'],'m' => $res[$i]['m']);
             $permissions[$res[$i]['name']] = $rights;
          }
        }
      }
    }
    return $permissions;
  }

}

?>
